/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package transervfx.Dato.DAO;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import transervfx.Dato.Entidad.Tramite;

/**
 *
 * @author oscar
 */
public class TramiteDAO implements Dao<Tramite, Long> {

    private static TramiteDAO INSTANCE = null;

    /**
     * Crea el Gestor Auxiliar
     */
    protected TramiteDAO() {
    }

    /**
     * creador sincronizado para protegerse de posibles problemas multi-hilo
     * otra prueba para evitar instanciaciÃ³n mÃºltiple
     */
    private synchronized static void createInstance() {
        if (INSTANCE == null) {
            INSTANCE = new TramiteDAO();
        }
    }

    /**
     * Obtiene la instancia.
     *
     * @return devuelve la instancia del "GestorAuxiliar".
     */
    public static TramiteDAO getInstance() {
        if (INSTANCE == null) {
            createInstance();
        }
        return INSTANCE;
    }

    @Override
    public void persist(Tramite entity) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {
            EntityTransaction t = em.getTransaction();
            try {
                t.begin();
                em.persist(entity);
                t.commit();
            } finally {
                if (t.isActive()) {
                    t.rollback();
                }
            }
        } finally {
            em.close();
        }
    }

    @Override
    //Sin transaccion
    public void remove(Tramite entity) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            em.remove(entity);

        } finally {
            em.close();
        }
    }

    @Override
    public void update(Tramite entity) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            em.getTransaction().begin();
            if (em.contains(entity)) {
                em.merge(entity);
            }
            em.getTransaction().commit();

        } finally {
            em.close();
        }
    }

    @Override
    public Tramite findById(Long id) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return em.getReference(Tramite.class, id);

        } finally {
            em.close();
        }
    }

    @Override
    public List<Tramite> findAll() {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return em.createNamedQuery("Tramite.findAll").getResultList();

        } finally {
            em.close();
        }
    }

    public Tramite findByCompleto(Tramite tramiteSeleccionado) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            Tramite aux = em.getReference(Tramite.class, tramiteSeleccionado);
            /*aux.getDomicilio();
             if( !aux.getZonasRegionals().isEmpty()) {
             aux.getZonasRegionals().get(0);
             }*/
            return aux;

        } finally {
            em.close();
        }
    }

    public Long numeroDeTramitesFiltrados() {

        String consulta = "SELECT COUNT(t) FROM Tramite t ";

        System.out.println(consulta);
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {
            Query query = em.createQuery(consulta);

            return (Long) query.getSingleResult();

        } finally {
            em.close();
        }
    }

    public List<Tramite> tramitesPendienteEntregaRango(int ini, int fin) {


        String consulta = "SELECT t FROM Tramite t";

        System.out.println(consulta);

        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {
            Query query = em.createQuery(consulta);
            query.setFirstResult(ini);
            query.setMaxResults(fin);

            return (List<Tramite>) query.getResultList();

        } finally {
            em.close();
        }
    }
}